package com.config;


import java.beans.PropertyVetoException;

import java.util.Properties;


import javax.annotation.PreDestroy;
import javax.sql.DataSource;

import org.apache.ibatis.io.VFS;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import com.common.util.SpringBootVFS;
import com.github.pagehelper.PageHelper;

@Configuration
@EnableConfigurationProperties(DataSourceProperties.class)
//mybaits dao 搜索路径
@MapperScan("com.dao")
public class MybatisDataSource {
	@Autowired
	private DataSourceProperties dataSourceProperties;
	//mybaits mapper xml搜索路径
	private final static String mapperLocations="classpath:mapper/**/*.xml"; 
	
	private String typeAliasesPackage = "com.entity";  

	private com.mchange.v2.c3p0.ComboPooledDataSource pool;
	
	@Bean(destroyMethod = "close")
	public DataSource dataSource() throws PropertyVetoException {	
		DataSourceProperties config = dataSourceProperties;		
		this.pool = new com.mchange.v2.c3p0.ComboPooledDataSource();	
		this.pool.setDriverClass(config.getDriverClassName());
		this.pool.setJdbcUrl(config.getUrl());
		if (config.getUsername() != null) {
			this.pool.setUser(config.getUsername());
		}
		if (config.getPassword() != null) {
			this.pool.setPassword(config.getPassword());
		}
		this.pool.setAcquireIncrement(config.getAcquireIncrement());
		this.pool.setInitialPoolSize(config.getInitialPoolSize());
		this.pool.setMinPoolSize(config.getMinPoolSize());
		this.pool.setMaxPoolSize(config.getMaxPoolSize());
		this.pool.setMaxStatements(config.getMaxStatements());
		this.pool.setMaxStatementsPerConnection(config.getMaxStatementsPerConnection());
		this.pool.setTestConnectionOnCheckin(true); 
		this.pool.setAutomaticTestTable("C3P0TestTable");
		this.pool.setIdleConnectionTestPeriod(18000);
		this.pool.setMaxIdleTime(20);
		this.pool.setTestConnectionOnCheckout(true);
		return this.pool;
	}

	@PreDestroy
	public void close() {
		if (this.pool != null) {
			this.pool.close();
		}
	}
	
	@Bean
	public SqlSessionFactory sqlSessionFactoryBean() throws Exception {		
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		sqlSessionFactoryBean.setDataSource(dataSource());		
		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		sqlSessionFactoryBean.setMapperLocations(resolver.getResources(mapperLocations));
		
		//瑙ｅ喅myBatis涓� 涓嶈兘宓屽jar鏂囦欢鐨勯棶棰�
		VFS.addImplClass(SpringBootVFS.class);
		sqlSessionFactoryBean.setTypeAliasesPackage(typeAliasesPackage);

        //添加分页插件 
		 sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageHelper()});
		 return sqlSessionFactoryBean.getObject();
	}
	
	@Bean
	public PlatformTransactionManager transactionManager() throws PropertyVetoException {
		return new DataSourceTransactionManager(dataSource());
	}
	
    @Bean
    public PageHelper pageHelper(){
        //分页插件
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("dialect", "mysql");
        properties.setProperty("offsetAsPageNum", "true");
        properties.setProperty("rowBoundsWithCount", "true");
        properties.setProperty("reasonable", "false");
        properties.setProperty("pageSizeZero", "true");
       // properties.setProperty("supportMethodsArguments", "true");
        properties.setProperty("params", "pageNum=start;pageSize=limit;");
        pageHelper.setProperties(properties);
        return pageHelper;
    }
}
